package com.zxl.grocery.microservice.crawler.pipeline;

import com.zxl.grocery.microservice.common.util.ToolUtil;
import com.zxl.grocery.microservice.crawler.entity.CrawlerNews;
import com.zxl.grocery.microservice.crawler.service.biz.CrawlerNewsBiz;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Repository;
import us.codecraft.webmagic.ResultItems;
import us.codecraft.webmagic.Task;
import us.codecraft.webmagic.pipeline.Pipeline;

import java.util.Date;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * @Author: Lzx
 * @Description: 新闻
 * @Date: Created in 9:43 2018/1/16
 * @Modified By:
 */
@Repository
public class NewsPipeline implements Pipeline {

    @Autowired
    private CrawlerNewsBiz crawlerNewsBiz;

    @Override
    public void process(ResultItems resultItems, Task task) {
        for (Map.Entry<String, Object> entry : resultItems.getAll().entrySet()) {
            if (entry.getKey().contains("news")) {
                CrawlerNews news=(CrawlerNews) entry.getValue();
                CrawlerNews param = new CrawlerNews();
                param.setLink(news.getLink());
                CrawlerNews crawlerNews = crawlerNewsBiz.selectOne(param);

                if (ToolUtil.isEmpty(crawlerNews)) {//检查链接是否已存在
                    news.setAuthor("zxl");
                    news.setSort(1);
                    news.setStatus(1);
                    news.setPlacedTop(false);
                    news.setRecommend(false);
                    news.setThumbUp(0);
                    news.setExplicitLink((byte) 1);
                    news.setCreateDate(new Date());
                    news.setModifyDate(new Date());
                    crawlerNewsBiz.insertSelective(news);
                }
            }

        }
    }
}
